﻿

namespace Kevin.Powershell.Commands
{
    using System.Management.Automation;
    using System.Net.NetworkInformation;
    using System.Collections.Generic;
    using cmdlets;

    #region GetNetAdapterCommand
    [Cmdlet(VerbsCommon.Get, "NetAdapter")]
    public class GetNedAdapterCommand : Cmdlet
    {

        #region Paramaters

        /// <summary>
        /// The names of the NICs retrieved by the cmdlet.
        /// </summary>
        public string[] nicNames = null;

        public netadapter[] adapters { get; private set; }


        /// <summary>
        /// Specifies the cmdlet Name parameter
        /// </summary>
        [Parameter()]
        [ValidateNotNullOrEmpty]
        public string[] Name
        {
           get { return this.nicNames; }
           set { this.nicNames = value; }
        }
        [Parameter()]
        [ValidateNotNullOrEmpty]
        public bool all
        {
            get { return this.all; }
            set { this.all = value; }
        }

        #endregion Parameters

        #region Cmdlet Overrides

        protected override void ProcessRecord()
        {
            base.ProcessRecord();
            NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();

            ///<summary>
            /// Make a nice formatted table of all network adapters, and assign them 
            /// better names
            /// </summary>
            var listOfAdapters = new List<netadapter>();
            for (int x = 0; x < interfaces.Length; x++)
            {
                listOfAdapters.Add(new netadapter());
                listOfAdapters[x].Name = "NIC" + x.ToString();
                listOfAdapters[x].fullName = interfaces[x].Name;
                listOfAdapters[x].interfaceDescription = interfaces[x].Description;
                listOfAdapters[x].MacAddress = formatMacAddress(interfaces[x].GetPhysicalAddress().ToString());
                listOfAdapters[x].status = interfaces[x].OperationalStatus.ToString();
                listOfAdapters[x].MediaType = interfaces[x].NetworkInterfaceType.ToString();

                listOfAdapters[x].LinkSpeed = interfaces[x].Speed.ToString();
                try
                {
                    IPInterfaceProperties adapterProperties = interfaces[x].GetIPProperties();
                    IPv4InterfaceProperties p = adapterProperties.GetIPv4Properties();
                    listOfAdapters[x].ifIndex = p.Index;
                }
                catch
                {
                    listOfAdapters[x].ifIndex = 0;
                }
                
                
            }

            /// If no interface is specified, show all interfaces
            /// that are up. NOTE: By default, only active interfaces
            /// are shown.
            if (this.nicNames == null)
            {
                for (int x = 0; x < listOfAdapters.Count; x++)
                {

                    if (listOfAdapters[x].status == "Up")
                    {
                        WriteObject(listOfAdapters[x], true);
                    }
                }   
                
            }
            /// If specific interfaces are specified, loop through
            /// and show each one.
            else
            {
                foreach (string name in this.nicNames)
                {
                    for (int x = 0; x < listOfAdapters.Count;x++)
                    {
                        if (listOfAdapters[x].Name == name)
                        {
                            WriteObject(listOfAdapters[x], true);
                        }
                    }
                    
                }

                
            }

        }
        #endregion Cmdlet Overrides

        /// <summary>
        /// Accept MAC address as string, format correctly, and return.
        /// </summary>
        /// <param name="Mac"></param>
        /// <returns>
        /// Fromatted MAC address (XX-XX-XX-XX-XX-XX)
        /// </returns>
        public string formatMacAddress(string Mac)
        {
            string formattedMac = "";
            if (Mac != null)
            {
                for (int i = 0; i < Mac.Length; )
                {
                    formattedMac += Mac[i++];
                    if (i == Mac.Length)
                    {
                        break;
                    }

                    formattedMac += Mac[i++];
                    if (i < Mac.Length)
                    {
                        formattedMac += '-';
                    }
                }
            }
            return formattedMac;

        }

    }


    #endregion GetNetAdapterCommand
}
